package com.scuec.groupfive.mapper;

import org.apache.ibatis.annotations.*;
import java.util.List;
import java.util.Map;

@Mapper
public interface CommunityAnalysisMapper {

    @Select("<script>" +
            "SELECT DATE_FORMAT(p.created_at, '%Y-%m-%d') AS date, " +
            "COUNT(DISTINCT p.post_id) AS postCount, " +
            "(SELECT COUNT(*) FROM post_comments pc WHERE DATE(pc.created_at) = DATE(p.created_at)) AS commentCount, " +
            "(SELECT COUNT(*) FROM post_likes pl WHERE DATE(pl.created_at) = DATE(p.created_at)) AS likeCount, " +
            "(SELECT COUNT(*) FROM post_favorites pf WHERE DATE(pf.created_at) = DATE(p.created_at)) AS favoriteCount " +
            "FROM posts p " +
            "WHERE p.created_at BETWEEN #{startDate} AND DATE_ADD(#{endDate}, INTERVAL 1 DAY) " +
            "GROUP BY date ORDER BY date" +
            "</script>")
    List<Map<String, Object>> getInteractionTrends(@Param("startDate") String startDate,
                                                   @Param("endDate") String endDate);

    @Select("<script>" +
            "SELECT type AS postType, COUNT(*) AS count " +
            "FROM posts " +
            "WHERE status = 1 " +
            "<if test='startDate != null and endDate != null'>" +
            "AND created_at BETWEEN #{startDate} AND DATE_ADD(#{endDate}, INTERVAL 1 DAY) " +
            "</if>" +
            "GROUP BY type" +
            "</script>")
    List<Map<String, Object>> getPostTypeDistributions(@Param("startDate") String startDate,
                                                       @Param("endDate") String endDate);
}